package com.questionnaire.controller;

import com.github.pagehelper.PageInfo;
import com.questionnaire.entity.*;
import com.questionnaire.service.AnswerService;
import com.questionnaire.service.QuestionService;
import com.questionnaire.service.QuestionnaireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
public class AnswerController {
    @Autowired
    private AnswerService answerService;
    @Autowired
    QuestionnaireService questionnaireService;
    @Autowired
    QuestionService questionService;
    //填写问卷路径
    @RequestMapping("/naire_fill.do/{nid}")
    public String  naire_fill(ModelMap map, @PathVariable("nid") Integer nid){

        Questionnaire questionnaire= questionnaireService.selectByPrimaryKey(nid);

        QuestionExample example=new QuestionExample();
        QuestionExample.Criteria criteria = example.createCriteria();
        criteria.andOwnerquestionnaireidEqualTo(nid);
        List<Question> questions= questionService.selectByExample(example);
        PageInfo<Question> pageInfo=new PageInfo<Question>(questions);

        map.put("questionnaire",questionnaire);
        map.put("pageInfo",pageInfo);
        return "naire_fill";
    }

    //提交答案
    @RequestMapping(value = "/addAnswer.do")
    public void   addAnswer(Answers answer) {

//        System.out.println(answer);
        answerService.insertSelective(answer);
    }
}
